package com.jf.car.controller;

import com.alibaba.fastjson.JSON;
import com.jf.car.bean.Userinfo;
import com.jf.car.service.UserinfoService;
import com.jf.car.service.impl.UserinfoServiceImpl;
import com.jf.car.util.MyConstants;
import com.jf.car.util.MyUtils;
import com.jf.car.util.PageInfo;
import com.jf.car.util.ResultJson;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userinfo")
public class UserinfoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String param = request.getParameter("param");
        UserinfoService userinfoService = new UserinfoServiceImpl();
        Userinfo userinfo = new Userinfo();

        userinfo.setId(MyUtils.string2Int(request.getParameter("id")));
        userinfo.setUsername(request.getParameter("username"));
        userinfo.setPassword(request.getParameter("password"));
        userinfo.setNickname(request.getParameter("nickname"));
        userinfo.setPhone(request.getParameter("phone"));
        userinfo.setRole(MyUtils.string2Int(request.getParameter("role")));
        userinfo.setDepartmentId(MyUtils.string2Int(request.getParameter("departmentId")));
        userinfo.setStatus(MyUtils.string2Int(request.getParameter("status")));
        userinfo.setAvatar(request.getParameter("avatar"));

        /*跳转用户列表*/
        /*要是 param 为空，条件就会成立*/
        if (MyUtils.isStringEmpty(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-list.jsp").forward(request,response);
        }

        /*分页*/
        if ("list".equals(param)){
            int page = MyUtils.string2Int(request.getParameter("page"),1);
            int pageSize = MyUtils.string2Int(request.getParameter("pageSize"),5);
            PageInfo pageInfo = new PageInfo(page,pageSize);
            response.getWriter().println(JSON.toJSON(userinfoService.getPage(pageInfo,userinfo)));
        }
        /*删除*/
        if("delete".equals(param)){
            response.getWriter().println(JSON.toJSON(userinfoService.delete(userinfo.getId())));
        }
        /*跳转添加页*/
        if ("edit".equals(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-edit.jsp").forward(request,response);
        }

        if ("info".equals(param)){
            response.getWriter().println(JSON.toJSON(ResultJson.success(userinfoService.getById(userinfo.getId()))));
        }
        /*更新数据*/
        if ("update".equals(param)){
            response.getWriter().println(JSON.toJSON(userinfoService.saveOrUpdate(userinfo)));
        }
        /*跳转更改密码页面*/
        if ("pwd".equals(param)){
            request.getRequestDispatcher("/WEB-INF/pages/admin/userinfo/userinfo-password.jsp").forward(request,response);
        }
        /*检查原密码是否正确*/
        if ("checkPwd".equals(param)){
            response.getWriter().println(JSON.toJSON(userinfoService.checkPassword(userinfo)));
        }
        /*更改密码成功后返回登录页面*/
        if ("logout".equals(param)){
            /*清除session作用域*/
            request.getSession().removeAttribute(MyConstants.LOGIN_SESSION_KEY);
            response.sendRedirect("login");
        }
        if ("del".equals(param)){
            String ids = request.getParameter("ids");
            response.getWriter().println(JSON.toJSON(userinfoService.batchDelete(ids)));
        }
    }
}
